package com.six.action.settings;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

import com.opensymphony.xwork2.ActionSupport;
import com.six.biz.IAdminManagementBiz;
import com.six.biz.IGroupManagementBiz;
import com.six.biz.IUserGroupBiz;
import com.six.biz.impl.settings.IAdminManagementBizImpl;
import com.six.biz.impl.settings.IGroupManagementBizImpl;
import com.six.biz.impl.settings.IUserGroupManagementBizImpl;
import com.six.vo.User;
import com.six.vo.UserGroup;

/**
 * AdminManagementAction 用户管理 Action
 * 
 */
@Namespace("/settings")  
@ParentPackage(value = "settings") 
public class AdminManagementAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 4954037840818227253L;
	private IAdminManagementBiz adminbiz = new IAdminManagementBizImpl(); //用户管理业务处理接口
	private IGroupManagementBiz grouBiz = new IGroupManagementBizImpl(); //组管理业务处理接口
	private IUserGroupBiz ugBiz = new IUserGroupManagementBizImpl(); //用户管理业务处理接口
	private List<User> UList = new ArrayList<User>();
	private User user;
	private int uid;
	private String uname;
	private String optMark;
	private String group;
	private String verifypass;
	
	public List<User> getUList() {
		return UList;
	}
	public void setUList(List<User> uList) {
		UList = uList;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public int getUid() {
		return uid;
	}
	public void setUid(int uid) {
		this.uid = uid;
	}
	public String getUname() {
		return uname;
	}
	public void setUname(String uname) {
		this.uname = uname;
	}
	public String getOptMark() {
		return optMark;
	}
	public void setOptMark(String optMark) {
		this.optMark = optMark;
	}
	public String getGroup() {
		return group;
	}
	public void setGroup(String group) {
		this.group = group;
	}
	public String getVerifypass() {
		return verifypass;
	}
	public void setVerifypass(String verifypass) {
		this.verifypass = verifypass;
	}
	/**
	 * 添加修改用户信息，页面输入校验方法
	 * 
	 */
	public void validateOptAdminManagement(){
		
	}
	/**
	 * 管理员/用户信息的添加或者修改
	 * 
	 */
	@Action(value = "userOptAction", 
			results = { 
				@Result(name = "fail", location = "/global/error.jsp") 
				,@Result(name = "success", location = "/manager/UserManagement.jsp")
				,@Result(name = "input", location = "/manager/UserManagement.jsp") 
			}
	)
	public String optAdminManagement() throws Exception {
		//添加
		validateOptAdminManagement();
		adminbiz.saveUser(user);
		UserGroup ug = new UserGroup();
		ugBiz.saveUserGroup(ug);
		//修改
		adminbiz.updateUser(user);
		ugBiz.deleteUserGroup("1");
		ugBiz.saveUserGroup(ug);
		return null;
		
	}
	/**
	 * 异步响应管理员/员工信息到页面
	 * 
	 */
	@Action(value = "queryUserInfoAction", 
			results = { 
				@Result(name = "fail", location = "/global/error.jsp") 
				,@Result(name = "success", location = "/manager/UserManagement.jsp")
				,@Result(name = "input", location = "/manager/UserManagement.jsp") 
			}
	)
	public String queryUserInfo() throws Exception {
		adminbiz.findUserById(uid);
		return null;
		
	}
	/**
	 * 管理员/员工列表
	 * 
	 */
	@Action(value = "queryUserListAction", 
			results = { 
				@Result(name = "fail", location = "/global/error.jsp") 
				,@Result(name = "success", location = "/settings/user_query.jsp")
				,@Result(name = "input", location = "/manager/UserManagement.jsp") 
			}
	)
	public String queryUserList() throws Exception {
//		uname = "三";
		uid = 1;
		UList = adminbiz.findUserlist(uid,uname);
		for (User u : UList) {
			System.out.println(u.toString());
		}
		return SUCCESS;
		
	}
	/**
	 * 删除管理员/员工,将状态置为删除状态即可，不是真的从数据库中移除数据
	 * 
	 */
	@Action(value = "deleteUserAction", 
			results = { 
				@Result(name = "fail", location = "/global/error.jsp") 
				,@Result(name = "success", location = "/manager/UserManagement.jsp")
			}
	)
	public String deleteUser() throws Exception {
		adminbiz.deleteUser(uid);
		return null;
		
	}
	/**
	 * 恢复管理员/员工
	 * 
	 */
	@Action(value = "revertUserAction", 
			results = { 
				@Result(name = "fail", location = "/global/error.jsp") 
				,@Result(name = "success", location = "/manager/UserManagement.jsp")
			}
	)
	public String revertUser() throws Exception {
		adminbiz.revertUser(uid);
		return null;
		
	}
	/**
	 * 检测用户名是否被使用
	 * 
	 */
	@Action(value = "findAccountIsUsedAction")
	public String findAccountIsUserdAction() throws Exception {
		return null;
		
	}

}
